#include "umlclassheader.h"

#include "uml/class/umlclassitem.h"


UmlClassHeader::UmlClassHeader(QGraphicsItem *parent) : UmlTextChildItem(parent)
{
    this->setFont(this->boldFont());

    this->_highlighter = new UmlClassHeaderHighlighter(this->document());

    this->_isHovered = false;
}

void UmlClassHeader::setClassItem(UmlClassItem *classItem)
{
    this->_classItem = classItem;

    this->updateUmlText();
}

void UmlClassHeader::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
    if (event->reason() == QGraphicsSceneContextMenuEvent::Mouse) {
        QList<QAction*> actionList = this->_classItem->contextActions();

        if (!actionList.isEmpty()) {
            QMenu menu;
            menu.addActions(actionList);
            menu.exec(event->screenPos());
        }
    }
}

void UmlClassHeader::openEditor()
{
    this->_classItem->openClassEditor();
}

void UmlClassHeader::updateUmlText()
{
    QString umlText = this->_name;

    if (this->_classItem->isAbstract()) {
        umlText = QString("%1<br/>{abstract}").arg(this->_classItem->name());
    }

    else if (this->_classItem->isInterface()) {
        umlText = QString("%1<br/>&lt;&lt;interface&gt;&gt;").arg(this->_classItem->name());
    }

    this->setHtml("<center>" + umlText + "</center>");
}
